package com.thinksns.sociax.t4.android.login;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.component.CustomTitle;
import com.thinksns.sociax.component.LeftAndRightTitle;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.android.setting.ActivitySettingAboutUs;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.thinksnsbase.activity.widget.LoadingView;
import com.thinksns.sociax.thinksnsbase.exception.ApiException;

/**
 * @author LeiYan
 * @describe 用户使用条款详细
 * @date 2017/1/5
 * @contact email:907238552@qq.com
 */
public class ActivityUseRuleDetail extends ThinksnsAbscractActivity {

    private WebView wv_about_us;
    protected UnitSociax uint;
    private ProgressBar pb_bar;

    private Handler mHandler;

    @Override
    public String getTitleCenter() {
        return "用户使用条款";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_use_rule_detail;
    }
    @Override
    protected CustomTitle setCustomTitle() {
        return new LeftAndRightTitle(R.drawable.img_back, this);
    }

    private void initView() {
        loadingView = (LoadingView) findViewById(LoadingView.ID);
        wv_about_us = (WebView) findViewById(R.id.wv_about_us);
        pb_bar = (ProgressBar) this.findViewById(R.id.pb_bar);
        uint = new UnitSociax(this);
        wv_about_us.setHorizontalScrollBarEnabled(false);    //设置滑动条水平不显示
        wv_about_us.setVerticalScrollBarEnabled(false);    //设置滑动条垂直不显示
    }

    private void initData() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    uint.appendWebViewContent(wv_about_us, (String) msg.obj, null);
                    wv_about_us.setWebChromeClient(new WebChromeClient() {

                        @Override
                        public void onProgressChanged(WebView view, int newProgress) {
                            if (newProgress == 100) {
                                pb_bar.setVisibility(View.INVISIBLE);
                            } else {
                                if (View.INVISIBLE == pb_bar.getVisibility()) {
                                    pb_bar.setVisibility(View.VISIBLE);
                                }
                                pb_bar.setProgress(newProgress);
                            }
                            super.onProgressChanged(view, newProgress);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.handleMessage(msg);
            }
        };
        getUserRule();
    }

    /**
     * 获取用户协议 url
     */
    private void getUserRule() {
        {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        Message msg = new Message();
                        try {
                            Thinksns app = (Thinksns) getApplicationContext();
                            msg.obj = app.getPublicApi().getUserAgreement();
                        } catch (ApiException e) {
                            e.printStackTrace();
                        }
                        mHandler.sendMessage(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
